<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Gio_Core_Translator
{
	public function __construct()
	{
	}
	
	/**
	 * @param string $key Key to translate
	 * @param string $module Name of module. If this is not specified, 
	 * it will take the current module
	 * @return string
	 */
	public function translator($key = null, $module = null, $lang = null)
	{
		$key = (string)$key;
		if (null == $key && null == $module) {
			return $this;
		}
		
		if (null == $module) {
			/**
			 * Get current module
			 */
			$module = Gio_Core_Request::getInstance()->getParam('module');
		}
		/**
		 * Get current language
		 */
		$lang = ($lang) ? $lang : self::_getLang();
		/**
		 * If we want to use the file in languages directory
		 * $file = ROOT_DIR . DS . 'languages' . DS . $lang . DS . 'modules' . DS . $module . DS . 'languages' . DS . 'lang.' . $lang . '.ini';		 
		 */
		
		$file = ROOT_DIR . DS . 'modules' . DS . $module . DS . 'languages' . DS . 'lang.' . $lang . '.ini';
		if (file_exists($file)) {
			$iniArray = @parse_ini_file($file);
			return (isset($iniArray[$key])) ? $iniArray[$key] : $key;
		} 
		return $key;
	}
	
	/**
	 * @param string $key Key to translate
	 * @param string $module Widget module name (since 2.1.0)
	 * @param string $widgetName Widget name (since 2.1.0)
	 * 
	 * @return string
	 */
	public function widget($key, $module = null, $widgetName = null, $lang = null)
	{
		$lang = ($lang) ? $lang : self::_getLang();
		
		if ($module == null || $widgetName == null) {
			$widget = Gio_Core_Widget::getIntance();
			/**
			 * Get current widget instance which have been set in
			 * __call() of Tomato_Widget
			 */
			if (!$widget->getModuleName() || !$widget->getWidgetName()) {
				return $key;
			}
			
			/**
			 * If we want to use the file in languages directory
			 * $file = ROOT_DIR . DS . 'languages' . DS . $lang . DS . 'modules' . DS . strtolower($widget->getModule()) . DS . 'widgets' . DS . strtolower($widget->getName()) . DS . 'lang.' . $lang . '.ini';
			 */
			
		 	$file = ROOT_DIR . DS . 'modules' . DS . strtolower($widget->getModuleName()) . DS  . 'widgets' . DS . strtolower($widget->getWidgetName()) . DS . 'lang.' . $lang . '.ini';
		} else {
			$file = ROOT_DIR . DS . 'modules' . DS . strtolower($module) . DS  . 'widgets' . DS . strtolower($widgetName) . DS . 'lang.' . $lang . '.ini';
		}
		
		if (file_exists($file)) {
			$iniArray = @parse_ini_file($file);
			return (isset($iniArray[$key])) ? $iniArray[$key] : $key;
		} 
		return $key;
	}
	
	private static function _getLang()
	{
		$route = Gio_Core_Route::getCurrentRoute(true);
		$configs = Gio_Core_Config_Xml::getConfig('web');
		$lang = $configs->language;
		if (isset($route['localization']['enable']) && $route['localization']['enable'] == true) {
			$request = Gio_Core_Request::getInstance();
			$lang = $request->getParam('lang');
		} 
		return $lang;
	}
}
